﻿Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Text
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls


<DefaultProperty("Text"), ToolboxData("<{0}:gridviewExtend runat=server></{0}:gridviewExtend>")> _
Public Class gridviewExtend
    Inherits GridView

    <Bindable(True), Category("Appearance"), DefaultValue(""), Localizable(True)> Property Text() As String
        Get
            Dim s As String = CStr(ViewState("Text"))
            If s Is Nothing Then
                Return String.Empty
            Else
                Return s
            End If
        End Get

        Set(ByVal Value As String)
            ViewState("Text") = Value
        End Set
    End Property

    Protected Overrides Sub RenderContents(ByVal writer As HtmlTextWriter)
        writer.Write(Text)
    End Sub

    Protected Overrides Function CreateChildControls(ByVal dataSource As System.Collections.IEnumerable, ByVal dataBinding As Boolean) As Integer
        Dim numberOfLines As Integer
        numberOfLines = MyBase.CreateChildControls(dataSource, dataBinding)

        'no data rows created, create empty table if enabled
        If (numberOfLines = 0) Then
            Dim myTable As New Table
            myTable.ID = Me.ID

            Dim myLine As GridViewRow = MyBase.CreateRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal)

            Dim myFields As DataControlField()
            ReDim myFields(Me.Columns.Count - 1)

            Me.Columns.CopyTo(myFields, 0)

            Me.InitializeRow(myLine, myFields)
            myTable.Rows.Add(myLine)

            'creating a linha vazia
            myLine = MyBase.CreateRow(-1, -1, DataControlRowType.DataRow, DataControlRowState.Normal)

            Dim myCells As New TableCell()
            myCells.ColumnSpan = Me.Columns.Count
            myCells.Width = Unit.Percentage(100)
            myCells.Controls.Add(New LiteralControl("Nada para exibir"))

            myLine.Cells.Add(myCells)
            myTable.Rows.Add(myLine)

            Me.Controls.Add(myTable)
            numberOfLines = myTable.Rows.Count

        End If


        Return numberOfLines
    End Function

End Class
